<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
   <meta charset="utf-8">
   <th:block th:replace="headfoot/head::head_ztree">引用公共的homefoot</th:block>
</head>
<body>
	<div class="container-fluid kj-tree-container">
		
		<div class="card kj-tree-container-card">

			<div class="card-body kj-tree-container-card-body">
				<div class="card-toolbar clearfix  kj-tree-container-card-toolbar">
					<button type="button" class="btn btn-default btn-sm kj-tree-btn-add" onclick="addNode()">
						<i class="iconfont icon-tianjia"></i>&ensp;添加
					</button>

					<button type="button" class="btn btn-default btn-sm kj-tree-btn-edit" onclick="editNode()">
						<i class="iconfont icon-bianjibiaodan"></i>&ensp;编辑
					</button>

					<button type="button" class="btn btn-default btn-sm kj-tree-btn-del" onclick="removeNode()">
						<i class="iconfont icon-shanchu"></i>&ensp;删除
					</button>
				</div>
				<ul
					class="ztree"
					data-url="/system/organization/listByPid"
					data-setting='orgTreeSetting'
				></ul>
			</div>
		</div>

		
	</div>

	<script th:inline="javascript" type="text/javascript">
	
	 function filter(treeId, parentNode, childNodes) {
     	if (!childNodes) return null;
     	childNodes = childNodes.data;
     	for (var i=0, l=childNodes.length; i<l; i++) {
     		if(childNodes[i].name){
     			childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
     		}
     	}
     	return childNodes;
     }
     
     function addNode() {
    	 var treeObj = $(".ztree").data("ztree");
     	var nodes = treeObj.getSelectedNodes();
     	var pid;
     	if (!nodes || nodes.length === 0) {
	        	pid = "";
     	} else if (nodes.length == 1) {
	        	pid = nodes[0].id;
     	} else {
     		common.confirm("请选择一个节点!", null, {title: "提示"})
		    return;
     	}
     	
     	var url = "url:" + base_url + "/system/organization/form?pid=" + pid;
     	common.confirm(url, function(){
     		if (!$("#organizationForm").valid()) {
 				return false;
 			}
 			
 			var resultFlag = false;
 			$("#organizationForm").ajaxSubmit({
 				async: false,
 				url: base_url + "/system/organization/save",
 				dataType: "json",
 				success: function(data) {
 					if (data.code == 0) {
 						resultFlag = true;
 					}
 					common.alert(data, function() {
 						treeObj.reAsyncChildNodes(nodes[0], "refresh", null);
 					}, true);
 				}
 			});
 			return resultFlag;
     	}, {title: '添加部门', closeIcon: true,columnClass: 'col-md-10 col-md-offset-0'})
     }
     
     function editNode() {
     	var treeObj = $(".ztree").data("ztree");
     	var nodes = treeObj.getSelectedNodes();
     	if (nodes.length != 1) {
     		common.confirm("请选择一个节点!", null, {title: "提示"});
	        return;
     	}
     	var id = nodes[0].id;
     	
     	var url = "url:" + base_url + "/system/organization/form?id=" + id;
     	common.confirm(url, function() {
  			if (!$("#organizationForm").valid()) {
  				return false;
  			}
  			
  			var resultFlag = false;
  			$("#organizationForm").ajaxSubmit({
  				async: false,
  				url: base_url + "/system/organization/save",
  				dataType: "json",
  				success: function(data) {
  					if (data.code == 0) {
  						resultFlag = true;
  					}
  					common.alert(data, function() {
  						treeObj.reAsyncChildNodes(nodes[0].getParentNode(), "refresh", null);
  					}, true);
  				}
  			});
  			return resultFlag;
  		}, {title: '编辑部门', closeIcon: true,columnClass: 'col-md-10 col-md-offset-0'});
     }
     
     function removeNode() {
     	var treeObj = $(".ztree").data("ztree");
     	var nodes = treeObj.getSelectedNodes();
     	if (nodes.length == 0) {
     		common.confirm("请选择节点!", null, {title: "提示"});
	        return;
     	}
     	var ids = new Array();
     	nodes.forEach(function(node) {
     		ids.push(node.id);
     	});
     	
     	common.confirm("确认删除吗？", function() {
	     	$request.post(base_url + "/system/organization/batchDelete", {
	     		data: {
	     			ids: ids
	     		},
	     		confirm: common.confirm
	     	}, function() {
	     		treeObj.reAsyncChildNodes(null, "refresh", null);
	     	});r
     	}, {title: "确认"});
     }
     
     
     
	var orgTreeSetting =  {
							"async":{
										"enable":true,
										"url": "/system/organization/listByPid",
										"autoParam":["id","name=n","level=lv"],
										"otherParam":{"otherParam":"zTreeAsyncTest"},
										"dataFilter":filter
									},
							"check":{
										"enable":false,
										"chkStyle":"checkbox",
										"chkboxType":{"Y":"ps","N":"ps"}
									}
						}; 
   </script>


 
	  <th:block th:replace="headfoot/foot::form_ztree_foot">引用公共的homefoot</th:block>  

</body>
</html>